<?php
    $this->layout('base.phtml', [
        'title' => _('Storing a link'),
        'canonical' => url_full('link collections', ['id' => $link->id]),
        'has_errors' => $form->isInvalid(),
        'modal_enabled' => true,
    ]);
?>

<div class="section">
    <div class="section__title">
        <h1 id="modal-title"><?= _('Storing a link') ?></h1>
    </div>

    <p class="section__intro">
        <?= protect($link->title) ?>
    </p>

    <form
        data-turbo-preserve-scroll
        method="post"
        action="<?= url('update link collections', ['id' => $link->id]) ?>"
        data-controller="autosave"
        data-action="turbo:submit-start@document->autosave#clear"
    >
        <?= $this->include('alerts/_error.phtml', ['message' => $form->error('@base')]) ?>

        <fieldset aria-describedby="collections-desc">
            <legend><?= _('Collections') ?></legend>

            <?php $collections_by_others = $form->collectionsByOthers(); ?>
            <?php if ($collections_by_others): ?>
                <?= $this->include('collections/_collections_by_others.phtml', [
                    'collections' => $collections_by_others,
                    'link' => $link,
                ]) ?>
            <?php endif; ?>

            <?= $this->include('collections/_selector.phtml', [
                'form' => $form,
            ]) ?>
        </fieldset>

        <div class="form-group">
            <input
                type="checkbox"
                id="is-hidden"
                name="is_hidden"
                <?= $form->is_hidden ? 'checked' : '' ?>
            />

            <label class="label--checkbox" for="is-hidden">
                <?= _('Hide this link in public collections') ?>
            </label>
        </div>

        <?php $notepad = $link->notepad(); ?>
        <?php if ($notepad && $link->user_id === $current_user->id): ?>
            <details class="form-details">
                <summary>
                    <?= _('You already annoted this link.') ?>
                </summary>

                <div class="form-details__content flow flow--large">
                    <article class="notepad flow flow--large">
                        <?php foreach ($notepad as $date_iso => $notes): ?>
                            <?php $date = new \DateTimeImmutable($date_iso); ?>
                            <div id="notepad-<?= $date_iso ?>" class="flow">
                                <p>
                                    <span class="notepad__date" href="#notepad-<?= $date_iso ?>">
                                        <time datetime="<?= $date_iso ?>">
                                            <?= _d($date, 'dd MMMM y') ?>
                                        </time>
                                    </span>
                                </p>

                                <?php foreach ($notes as $note): ?>
                                    <div class="notepad__container">
                                        <div class="notepad__note">
                                            <?= $note->contentAsHtml() ?>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endforeach; ?>
                    </article>

                    <?= $this->include('links/collections/_new_note_form.phtml', [
                        'form' => $form,
                    ]) ?>
                </div>
            </details>
        <?php else: ?>
            <?= $this->include('links/collections/_new_note_form.phtml', [
                'form' => $form,
            ]) ?>
        <?php endif; ?>

        <div class="form__actions">
            <button type="submit" class="button--primary">
                <?php if ($form->mark_as_read): ?>
                    <?= _('Store the link and mark as read') ?>
                <?php else: ?>
                    <?= _('Store the link') ?>
                <?php endif; ?>
            </button>
        </div>

        <input type="hidden" name="csrf_token" value="<?= csrf_token($form) ?>" />
        <input type="hidden" name="mark_as_read" value="<?= $form->mark_as_read ?>" />
    </form>
</div>
